package com.svo.weitu;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.svo.platform.utils.PicUtil;
import com.svo.weitu.activity.MainActivity;
import com.svo.weitu.activity.fragment.LineFragment;
import com.svo.weitu.activity.fragment.TuoFragment;
import com.svo.weitu.model.entity.MenuEntity;

public class MenuAdapter extends BaseAdapter {
	private List<MenuEntity> entities;
	private Context context;
	private int selectPos;
	public void setSelectPos(int selectPos) {
		this.selectPos = selectPos;
	}
	public MenuAdapter(Context context , List<MenuEntity> entities,int position) {
		this.context = context;
		this.entities = entities;
		this.selectPos = position;
	}
	public MenuAdapter(Context context , List<MenuEntity> entities) {
		this(context, entities, 0);
	}
	@Override
	public int getCount() {
		return entities.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.channel_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.linearLayout = (LinearLayout) convertView.findViewById(R.id.selected_line);
			viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
			viewHolder.title = (TextView) convertView.findViewById(R.id.name);
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final MenuEntity entity = entities.get(position);
		if (!TextUtils.isEmpty(entity.getUri())) {
			PicUtil.displayImage(context, entity.getUri(), viewHolder.imageView);
		}
		viewHolder.title.setText(entity.getTitle());
		if (position == this.selectPos) {
			viewHolder.linearLayout.setVisibility(View.VISIBLE);
			convertView.setBackgroundResource(R.drawable.channel_list_item_bg_selected);
			viewHolder.title.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
		}else {
			viewHolder.linearLayout.setVisibility(View.INVISIBLE);
			convertView.setBackgroundResource(R.drawable.channel_list_item_bg);
		}
		
		final int tmp = position;
		convertView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				selectPos = tmp;
				notifyDataSetInvalidated();
				MainActivity activity = (MainActivity) context;
				Fragment rightFragment;
				if (entity.getId() < 4) {
					rightFragment = new LineFragment();
				}else {
					rightFragment = new TuoFragment();
				}
				Bundle args = new Bundle();
//				args.putInt("type", entity.getType());
				args.putString("title", entity.getTitle());
				args.putInt("id", entity.getId());
				args.putString("url", entity.getUrl());
				rightFragment.setArguments(args);
				activity.showRight(rightFragment);
			}
		});
		return convertView;
	}
	private static class ViewHolder{
		LinearLayout linearLayout;
		ImageView imageView;
		TextView title;
	}
}
